// pages/shopcar/shopcar.js
import { getApi } from "../../utils/API.js"

Page({
  /**
   * 页面的初始数据
   */
  data: {
    count: null,
    num: 1,//商品数量
    storechecked: false,
    prochecked: false,
    allprice: 0.00,
    carlist: [],
    allchecked: false,
    temId: "",//获取到的临时存放的id
  },
  //自定义函数
  //数目减少
  del(e) {
    console.log("aaa")
    console.log(e.currentTarget.dataset.i)

    if (this.data.carlist[e.currentTarget.dataset.i].num <= 1) {
      this.data.carlist[e.currentTarget.dataset.i].num = 1
    } else {
      this.data.carlist[e.currentTarget.dataset.i].num = this.data.carlist[e.currentTarget.dataset.i].num - 1
    }
    let arr = this.data.carlist
    this.setData({
      carlist: arr
    })
    this.allprice()
  },
  //数目增加
  add(e) {
    this.data.carlist[e.currentTarget.dataset.i].num = this.data.carlist[e.currentTarget.dataset.i].num + 1
    // console.log(this.data.carlist)
    // 本质数据以改变 但是我们小程序不可以直接改变,需要第三方变量来取值，然后改变
    let arr1 = this.data.carlist
    this.setData({
      carlist: arr1
    })
    this.allprice()
  },
  //点击全选。则商品都被选中
  selectAll() {
    let carlist = this.data.carlist
    if (this.data.allchecked == false) {
      this.setData({
        allchecked: true
      })
      carlist.forEach((item) => {
        item.checked = true
      })
      // console.log("商品全选")
    } else {
      this.setData({
        allchecked: false
      })
      carlist.forEach((item) => {
        item.checked = false
      })
      // console.log("商品取消选择")
    }
    this.setData({
      carlist: carlist
    })
  },
  //点击单个商品，
  changeSingle(e) {
    console.log(this.data.carlist)
    let index = e.currentTarget.dataset.index
    //  let allprice=

    // let allprice=
    if (this.data.carlist[index].checked == false) {
      console.log("false")

      this.data.carlist[index].checked = true

    } else {
      console.log("true")
      this.data.carlist[e.currentTarget.dataset.index].checked = false
    }
    this.ifcheck()
    this.allprice()
    // let allprice=
    // 计算总价


  },
  //是否为为全选
  ifcheck() {
    let flag = this.data.carlist.every(item => {
      return item.checked
    })
    // console.log(flag)
    if (flag == true) {
      this.setData({
        allchecked: true
      })
    } else {
      this.setData({
        allchecked: false
      })
    }
  },
  //计算总价
  allprice() {
    console.log(this.data.carlist)
    let allprice = null
    for (let i in this.data.carlist) {
      if (this.data.carlist[i].checked == true) {
        console.log(i)
        allprice += this.data.carlist[i].prod_price * this.data.carlist[i].num
      }
    }
    this.setData({
      allprice: allprice
    })
    console.log(allprice)
    // if(this.data.carlist[index].checked==true){

    //   let allprice =this.data.carlist[index].prod_price*this.data.carlist[index].num
    //   console.log(allprice)
    // }
  },
  //跳转页面。去确认订单页面
  goConfirm() {
    // console.log(this.data.carlist)
    for (let i in this.data.carlist) {
      if (this.data.carlist[i].checked == true) {
        console.log(this.data.carlist[i])
        this.addTemCar(this.data.carlist[i].user_id, this.data.carlist[i].prod_id, this.data.carlist[i].num)
        setTimeout(() => {
          wx.navigateTo({
            url: '../confirmOrder/confirmOrder?temId=' + this.data.temId+'&from=1',
          })
          // console.log(this.data.temId)
        }, 100)
      }
    }


  },
  //上传到临时购物车接口
  addTemCar(user_id, prod_id, order_num) {
    let data = {
      user_id: user_id,
      prod_id: prod_id,
      order_num: order_num,
    }
    getApi("/addTmepShop", data).then(res => {
      // console.log("临时存放成功")
      // console.log("存入时的临时订单id")
      this.setData({
        temId: res.data.data.insertId
      })
      console.log(this.data.temId)
    }).catch(err => {
      console.log("临时存放失败")
    })
  },
  //获取数据
  getData() {
    let data = {
      user_id: 1
    }
    getApi("/getShopCarList", data).then(res => {
      //添加checked字段，将ischecked的值对应改变为true和false
      res.data.data.forEach(item => {
        if (item.ischecked == 0) {
          item.checked = false
        } else {
          item.checked = true
        }
      })
      this.setData({
        carlist: res.data.data,
        count: res.data.data.length
      })
    }).catch(err => {
      console.log("购物车列表获取失败")
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getData()

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})